package com.ktgj.customer.controller;

import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.web.page.TableDataInfo;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.Rightdetail;
import com.ktgj.customer.domain.vo.RightDetailServicesVo;
import com.ktgj.customer.service.IRightdetailService;
import com.ktgj.service.api.RemoteService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 权益Controller
 *
 * @author ruoyi    @date 2021-07-30
 */
@ControllerAdvice
@RestController
@RequestMapping("/rightdetail")
public class RightdetailController extends BaseController
{
    @Autowired
    private IRightdetailService rightdetailService;

    @Autowired
    private RemoteService remoteService; //远程调用服务


    /**
     * 查询权益列表
     */
    //@PreAuthorize(hasPermi = "customer:rightdetail:list")
    @GetMapping("/list")
    public TableDataInfo list(Rightdetail rightdetail)
    {
        startPage();
        List<Rightdetail> list = rightdetailService.selectRightdetailList(rightdetail);
        return getDataTable(list);
    }

    /**
     * 导出权益列表
     */
    //@PreAuthorize(hasPermi = "customer:rightdetail:export")
    @Log(title = "权益", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Rightdetail rightdetail) throws IOException
    {
        List<Rightdetail> list = rightdetailService.selectRightdetailList(rightdetail);
        ExcelUtil<Rightdetail> util = new ExcelUtil<Rightdetail>(Rightdetail.class);
        util.exportExcel(response, list, "权益数据");
    }

    /**
     * 获取权益详细信息
     */
    //@PreAuthorize(hasPermi = "customer:rightdetail:query")
    @GetMapping(value = "/{rightdetailId}")
    public AjaxResult getInfo(@PathVariable("rightdetailId") Long rightdetailId)
    {
        return AjaxResult.success(rightdetailService.selectRightdetailById(rightdetailId));
    }

    /**
     * 新增权益
     */
    //@PreAuthorize(hasPermi = "customer:rightdetail:add")
    @Log(title = "权益", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Rightdetail rightdetail)
    {
        return toAjax(rightdetailService.insertRightdetail(rightdetail));
    }

    /**
     * 修改权益
     */
    //@PreAuthorize(hasPermi = "customer:rightdetail:edit")
    @Log(title = "权益", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Rightdetail rightdetail)
    {
        return toAjax(rightdetailService.updateRightdetail(rightdetail));
    }

    /**
     * 删除权益
     */
    //@PreAuthorize(hasPermi = "customer:rightdetail:remove")
    @Log(title = "权益", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rightdetailIds}")
    public AjaxResult remove(@PathVariable Long[] rightdetailIds)
    {
        return toAjax(rightdetailService.deleteRightdetailByIds(rightdetailIds));
    }

    /**
     * 根据服务类型查询服务范围
     *  参数范围 1 ~ 4
     *   1：贵宾厅
     *   2：机场服务
     *   3：值机
     *   4：vip通道
     * @param serviceType
     * @return
     */
    @ApiOperation("根据服务类型查询服务范围")
    @GetMapping("/getRangeByServiceType")
    public AjaxResult getRangeByServiceType(@RequestParam("serviceType") Long serviceType){

        List list = null;

        if(serviceType == 1){ //贵宾厅

            list = remoteService.getViphallList();

        }else if(serviceType == 2){ //机场服务

            list = remoteService.getAirportServiceList();

        }else if(serviceType == 3){ //值机

            list = remoteService.getVipboardingList();

        }else if(serviceType == 4){ //vip通道

            list = remoteService.getVipchannelList();
        }

        if(list == null){
            return AjaxResult.error("获取权益服务范围列表失败！");
        }

        return AjaxResult.success(list);
    }

    /**
     * 添加权益服务范围
     */
    @PostMapping("/setRightDetailService")
    public AjaxResult setRightDetailService(@RequestBody RightDetailServicesVo rightDetailServicesVo) {
        //根据权益ID，服务类型，服务ID创建关联关系
        return toAjax(rightdetailService.saveRightDetailService(rightDetailServicesVo));
    }


    /**
     * 删除权益服务范围
     * @param rightDetailId 权益id
     * @param serviceType 服务类型
     * @param serviceId 服务id
     * @return
     */
    @DeleteMapping("/removeRightDetailService")
    public AjaxResult removeRightDetailServiceChange(@RequestParam("rightDetailId") Long rightDetailId,
                                                     @RequestParam("serviceType") Long serviceType,
                                                     @RequestParam("serviceId") Long[] serviceId){

        int count = rightdetailService.removeRightDetailServiceChange(rightDetailId,serviceType,serviceId);

        return toAjax(count);
    }


}
